package com.jilian.mydemo.modul.order.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.ServiceProcessDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.utils.DateUtil;

import java.text.ParseException;
import java.util.Date;
import java.util.List;


public class SeeServiceAdapter extends RecyclerView.Adapter<SeeServiceAdapter.ViewHolder> {
    private Activity mContext;
    private List<ServiceProcessDto> datas;
    private CustomItemClickListener listener;



    public SeeServiceAdapter(Activity context, List<ServiceProcessDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_seeservice, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if(position==0){
            holder.vOne.setVisibility(View.INVISIBLE);
        }
        else if(position==datas.size()-1){
            holder.vTwo.setVisibility(View.INVISIBLE);
        }
        else{
            holder.vOne.setVisibility(View.VISIBLE);
            holder.vTwo.setVisibility(View.VISIBLE);
        }
        holder.tvName.setText(datas.get(position).getProcessName());
        try {
            Date date = DateUtil.stringToDate("yyyy-MM-dd HH:mm:ss",datas.get(position).getCreateTime());
            holder.tvHour.setText(DateUtil.dateToString("HH:mm",date));
            holder.tvDay.setText(DateUtil.dateToString("yyyy-MM-dd",date));
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        private View vOne;
        private View vTwo;
        private TextView tvHour;
        private TextView tvDay;
        private TextView tvName;






        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            vOne = (View) itemView.findViewById(R.id.v_one);
            vTwo = (View) itemView.findViewById(R.id.v_two);
            tvHour = (TextView)itemView. findViewById(R.id.tv_hour);
            tvDay = (TextView) itemView.findViewById(R.id.tv_day);
            tvName = (TextView)itemView. findViewById(R.id.tv_name);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });


        }

    }
}
